#include <clptcHostDoCmd.h>
#include <clptcDlgItf.h>
#include <clptcParse.h>
#include <clptcCmdList.h>

#if CLPTC_CPL_HOST_ENABLE
int clptcHostDoCmdRun(clptcHostDoCmdOpt *doCmdPtr)
{
    if (!(doCmdPtr && doCmdPtr->funptr && doCmdPtr->funptr->lpType == clptcFunType_Host))
    {
        return 0;
    }
    clptcHostCmdData tmpcmd;
    clptcDlgItfStc *tmpComDlg = &doCmdPtr->cmdDlg;
    if (queObjRead(doCmdPtr->cmdFifo, &tmpcmd, 0) <= 0)
    {
        return -1;
    }
    logdebugc("do cmd id=%d", tmpcmd.cmdid);
    switch (tmpcmd.cmdid)
    {
    case clptcCmdEnum_rwSn:
    {
        clptcCmdStcSnd_rwSn tmpSndData;
        genRandStrNorm(tmpSndData.wSn, sizeof(tmpSndData.wSn));
        clptcCmdFuncHostPack_rwSn(clptcStcPtr(doCmdPtr->funptr->lpPkrPsr), 0xff, clptcCrypte_None, &tmpSndData);
        if (clptcDlgDoCmd(tmpComDlg, 200) != 1)
        {
            logdebugc("");
        }
        break;
    }
    case clptcCmdEnum_rwAddr:
    {
        clptcCmdFuncHostPack_rwAddr(clptcStcPtr(doCmdPtr->funptr->lpPkrPsr), 0xff, clptcCrypte_None, (clptcCmdStcSnd_rwAddr *)tmpcmd.cmddt);
        if (clptcDlgDoCmd(tmpComDlg, 200) != 1)
        {
            logdebugc("");
        }
        break;
    }
    case clptcCmdEnum_getInf:
    {
        clptcCmdStcSnd_getInf tmpSndData;
        clptcCmdFuncHostPack_getInf(clptcStcPtr(doCmdPtr->funptr->lpPkrPsr), tmpcmd.cmddt[0], clptcCrypte_None, &tmpSndData);
        if (clptcDlgDoCmd(tmpComDlg, 200) != 1)
        {
            logdebugc("");
        }
        break;
    }
    case clptcCmdEnum_heartBeat:
    {
        clptcCmdStcSnd_heartBeat tmpheart;
        snprintf(tmpheart.hdata, sizeof(tmpheart.hdata), "com heart[%d]", tmpcmd.cmddt[0]);
        clptcCmdFuncHostPack_heartBeat(clptcStcPtr(doCmdPtr->funptr->lpPkrPsr), tmpcmd.cmddt[0], clptcCrypte_None, &tmpheart);
        if (clptcDlgDoCmd(tmpComDlg, 200) != 1)
        {
            logdebugc("");
        }
        break;
    }
    case clptcCmdEnum_wrtFileBeg:
    {
        tmpComDlg->dlgAddr = tmpcmd.cmddt[0];
        clptcDlgCmdWrtFile(tmpComDlg, "chkSndFile.txt", 1);
        break;
    }
    case clptcCmdEnum_readFileBeg:
    {
        tmpComDlg->dlgAddr = tmpcmd.cmddt[0];
        clptcDlgCmdReadFile(tmpComDlg, CLPTC_CHK_FILE_SAVE_DIR "hostRcvFile_1.txt", 1);
        break;
    }
    case 0xff00:
    {
        uint8_t tmpAddrs[4] = {1, 2, 3, 4};
        clptcDlgMgrSetAddrs(doCmdPtr->funptr->lpDlgMgr, tmpAddrs, sizeof(tmpAddrs));
        break;
    }
    case 0xff01:
    {
        uint8_t tmpAddrs[4] = {1, 2, 3, 0};
        clptcDlgMgrSetAddrs(doCmdPtr->funptr->lpDlgMgr, tmpAddrs, sizeof(tmpAddrs));
        break;
    }
    default:
    {
        logdebugc("not surport cmd:%d", tmpcmd.cmdid);
        break;
    }
    }
    return 0;
}

int clptcHostDoCmdInit(clptcHostDoCmdOpt *doCmdPtr, clptcFunItfStc *funptr)
{
    if (!(funptr && doCmdPtr))
    {
        logdebugc("");
        return -1;
    }
    doCmdPtr->funptr = funptr;
    clptcDlgItfInit(&doCmdPtr->cmdDlg, funptr);
    queObjInit(doCmdPtr->cmdFifo, sizeof(doCmdPtr->cmdFifo), sizeof(clptcHostCmdData), 1);
    return 1;
}
#endif
